package cn.tedu.admin.mall.handler;

import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

//import javax.validation.ConstraintViolation;
//import javax.validation.ConstraintViolationException;


/**
 * 全局异常处理器
 *
 * @author java@tedu.cn
 * @version 3.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    public GlobalExceptionHandler() {
        log.info("创建全局异常处理器对象：GlobalExceptionHandler");
    }

    @ExceptionHandler
    public JsonResult handleServiceException(ServiceException e) {
        log.debug("全局异常处理器开始处理ServiceException");
        return JsonResult.fail(e);
    }

    @ExceptionHandler
    public JsonResult handleServiceException(StringIndexOutOfBoundsException e) {
        log.debug("全局异常处理器开始处理StringIndexOutOfBoundsException");
        log.error("出现异常",e);
        return JsonResult.fail(ServiceCode.INVOKE_ERROR_UNKNOWN,"网不好,一会再试");
    }


    @ExceptionHandler
    public JsonResult ConstraintViolationException(ConstraintViolationException e) {
        log.debug("全局异常处理器开始处理StringIndexOutOfBoundsException");
        log.error("出现异常",e);
        log.error(e.getMessage());
        return JsonResult.fail(ServiceCode.ERROR_BAD_REQUEST, e.getMessage());
    }

    @ExceptionHandler
    public JsonResult IllegalArgumentException(IllegalArgumentException e) {
        log.debug("全局异常处理器开始处理StringIndexOutOfBoundsException");
        log.error("出现异常",e);
        log.error(e.getMessage());
        return JsonResult.fail(ServiceCode.ERROR_BAD_REQUEST, e.getMessage());
    }




    @ExceptionHandler
    public JsonResult handleBindException(BindException e) {
        log.debug("全局异常处理器开始处理BindException");
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("请求参数格式错误，");
        stringBuilder.append(e.getFieldError().getDefaultMessage());
        stringBuilder.append("！");
        String message = stringBuilder.toString();
        log.warn(message);
        log.error("bind-error",e);
        return JsonResult.fail(ServiceCode.ERROR_BAD_REQUEST, message);
    }



    @ExceptionHandler
    public JsonResult handleThrowable(Throwable e) {
        log.debug("全局异常处理器开始处理Throwable");
        log.debug("异常跟踪信息如下：", e);
        String message = "服务器忙，请稍后再试！【同学们，看到这句时，你应该检查服务器端的控制台，并在全局异常处理器中添加处理异常的方法】";
        return JsonResult.fail(ServiceCode.ERROR_UNKNOWN, message);
    }

}
